package com.spark.check

import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.view.View
import android.widget.ImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.spark.check.model.InviteLeaderListModel
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.server
import com.spark.common.ext.toast
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import kotlinx.android.synthetic.main.frag_invite_leader.*

/**
 * 特邀领导
 */
class InviteLeaderFragment : BaseFragment() {
    private lateinit var innerAdapter: InnerLeaderAdapter
    private lateinit var outerAdapter: OuterLeaderAdapter

    override fun contentResId(): Int {
        return R.layout.frag_invite_leader
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { pop() }

        innerAdapter = InnerLeaderAdapter(R.layout.item_inner_leader)
        outerAdapter = OuterLeaderAdapter(R.layout.item_outer_leader)

        recy_inner_leaders.adapter = innerAdapter
        recy_inner_leaders.layoutManager = object : GridLayoutManager(context, 4) {
            override fun canScrollHorizontally(): Boolean {
                return false
            }

            override fun canScrollVertically(): Boolean {
                return false
            }
        }

        recy_outer_leaders.adapter = outerAdapter
        recy_outer_leaders.layoutManager = object : GridLayoutManager(context, 4) {
            override fun canScrollHorizontally(): Boolean {
                return false
            }

            override fun canScrollVertically(): Boolean {
                return false
            }
        }

        fetchInvitedLeaders()
    }

    /**
     * 获取特邀领导
     */
    private fun fetchInvitedLeaders() {
        val params = mutableMapOf(
                Pair(Const.qr_taskid, "$id")
        )
        activity?.apply {
            Http.request()
                    .rxPost("${Const.base_url}${Const.act_task_leader_list}", params)
                    .wrapIO()
                    .subscribe(
                            {
                                Gson().fromJson(it, InviteLeaderListModel::class.java)?.apply {
                                    if (code == "200") {
                                        innerAdapter.addData(data.inuser)
                                        outerAdapter.addData(data.outuser)
                                    } else {
                                        toast(msg)
                                    }
                                }
                            },
                            {
                                hideLoader()
                            },
                            {
                                hideLoader()
                            },
                            {
                                dispose = it
                                loading()
                            }
                    )
        }

    }
}

class InnerLeaderAdapter(layout: Int) : BaseQuickAdapter<InviteLeaderListModel.Data.Inuser, BaseViewHolder>(layout) {
    override fun convert(helper: BaseViewHolder?, item: InviteLeaderListModel.Data.Inuser?) {
        if (item == null) return
        helper?.apply {
            getView<ImageView>(R.id.iv_avatar).server(item.pictureurl)
            setText(R.id.tv_leader_name, item.name)
            setText(R.id.tv_leader_career, item.departmentnames)
        }
    }
}

class OuterLeaderAdapter(layout: Int) : BaseQuickAdapter<InviteLeaderListModel.Data.Outuser, BaseViewHolder>(layout) {
    override fun convert(helper: BaseViewHolder?, item: InviteLeaderListModel.Data.Outuser?) {
        if (item == null) return
        helper?.apply {
            setText(R.id.tv_leader_name, item.name)
        }
    }
}